package com.matianle.springboot_code1.service;

import com.matianle.springboot_code1.pojo.User;
import com.matianle.springboot_code1.pojo.dto.UserDto;
import com.matianle.springboot_code1.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService{
    @Autowired
    UserRepository userRepository;
    @Override
    public User add(UserDto user) {
        User userPojo=new User();
        //数据转换 ---user 转换userPojo
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }

    /**
     *查询用户
     * @param userId
     * @return
     */
    @Override
    public User getUser(Integer userId) {
        userRepository.findById(userId).orElseThrow(() ->{
            return new IllegalArgumentException("用户不存在,参数异常");
        });
        return null;
    }


    @Override
    public User edit(UserDto user) {
        User userPojo=new User();
        //数据转换 ---user 转换userPojo
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);

    }

    @Override
    public void delete(Integer userId) {
        userRepository.deleteById(userId);
    }





}
